package com.maven.front.controller.eduQuestions;


import com.maven.front.bean.EduQuestions;
import com.maven.front.bean.EduQuestionsComment;
import com.maven.front.bean.EduUser;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduQuestionsDto;
import com.maven.front.service.eduQuestions.EduQuestionsService;
import com.maven.front.service.eduQusestionsComment.EduQuestionsCommentService;
import com.maven.front.service.eduUser.EduUserService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;


@Slf4j
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("admin/eduq")
public class EduQuestionsController {

    @Autowired
    private EduQuestionsService eduQuestionsService;
    @Autowired
    private EduUserService eduUserService;
    @Autowired
    private EduQuestionsCommentService eduQuestionsCommentService;

    //查询所有或者条件查询
    @PostMapping("listAll")
    public Result listAll(EduQuestions eduQuestions) throws  Exception {
         if (eduQuestions.getStartDate()!=null){
            eduQuestions.setStartDate(toDate(eduQuestions.getStartDate()));
        }
         if (eduQuestions.getEndDate()!=null){
             eduQuestions.setEndDate(toDate(eduQuestions.getEndDate()));
         }
        List<EduQuestionsDto> list =eduQuestionsService.listAll(eduQuestions);
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1005, "查询失败", null);
        } else {

            return new Result(200,"查询成功",list);
        }
    }

    @GetMapping("selectid")
    public Result selectid(Integer id){
        List<EduQuestionsComment> listedua=eduQuestionsCommentService.selectByQuestionId(id);
      /*  System.out.println(eduQuestions.getUserName());*/
        if (listedua!=null){
            return new Result(200,"查询成功",listedua);
        }else {
            return new Result(1005, "查询失败", null);
        }
    }


    @GetMapping("getid")
    public Result getid(Integer id){
         EduQuestions eduQuestions=eduQuestionsService.getid(id);
        /*  System.out.println(eduQuestions.getUserName());*/
        if (eduQuestions!=null){
            return new Result(200,"查询成功",eduQuestions);
        }else {
            return new Result(1005, "查询失败", null);
        }
    }


    @PostMapping("update")
    public Result updat1(EduQuestions eduQuestions){
        int a=eduQuestionsService.update1(eduQuestions);
        if (a>0){
            return new Result(200,"成功");
        }else {
            return new Result(1009,"失败",null);
        }
    }


    @GetMapping("delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        int a=eduQuestionsService.delete(id);
        if (a>0){
            return new Result(200,"成功");
        }else {
            return new Result(1009,"失败",null);
        }
    }


    //    日期格式转换
    public Date toDate(Date date) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strdate = sdf.format(date);
        Date adddate = sdf.parse(strdate);
        return adddate;
    }



}
